package com.zznode.redsun.commons.sercurity.service;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.zznode.csp.domain.commons.persistence.EntityManager;
import com.zznode.redsun.commons.sercurity.model.Account;
import com.zznode.redsun.commons.sercurity.model.Permission;
import com.zznode.redsun.commons.sercurity.model.Role;
import com.zznode.redsun.smartcrm.model.PersonalInfo;

@Service
public class SecurityServiceImpl implements SecurityService {
	@Autowired
	private EntityManager entityManager;

	@Override
	public void saveRole(Role role) {
		Assert.notNull(role, "Saved instance can't be null.");
		if (null != role.getId()) {
			Role instance = entityManager.get(Role.class, role.getId());
			instance.setCode(role.getCode().toUpperCase());
			instance.setDescription(role.getDescription());
			instance.setName(role.getName());
			entityManager.update(instance);
		} else {
			role.setCode(role.getCode().toUpperCase());
			entityManager.save(role);
		}
	}

	@Override
	public void addPermission(Long roleId, String... permissionIds) {
		Assert.notNull(roleId, "Role id can't be empty.");
		Assert.isTrue(ArrayUtils.isNotEmpty(permissionIds), "Permission ids can't be empty.");

		Role role = entityManager.get(Role.class, roleId);
		for (String item : permissionIds) {
			role.addPermission(entityManager.load(Permission.class, Long.valueOf(item)));
		}
		entityManager.update(role);
	}

	@Override
	public void removePermission(Long roleId, String... permissionIds) {
		Assert.notNull(roleId, "Role id can't be empty.");
		Assert.isTrue(ArrayUtils.isNotEmpty(permissionIds), "Permission ids can't be empty.");

		Role role = entityManager.get(Role.class, roleId);
		for (String item : permissionIds) {
			role.removePermission(entityManager.load(Permission.class, Long.valueOf(item)));
		}
		entityManager.update(role);
	}

	@Override
	public void saveAccount(Account account) {
		Assert.notNull(account, "Saved instance can't be null.");
		if (null != account.getId()) {
			Account instance = entityManager.get(Account.class, account.getId());
			instance.setCode(account.getCode());
			instance.setDescription(account.getDescription());
			instance.setEnabled(account.getEnabled());
			instance.setName(account.getName());
			instance.setPassword(account.getPassword());
			this.entityManager.update(instance);
		} else {
			PersonalInfo personal = new PersonalInfo();
			personal.setCode(account.getCode());
			personal.setName(account.getName());
			this.entityManager.save(personal);
			account.setUserId(personal.getId());
			this.entityManager.save(account);
		}
	}

	@Override
	public void addRoles(Long accountId, String... roleIds) {
		Assert.notNull(accountId, "Account id can't be empty.");
		Assert.isTrue(ArrayUtils.isNotEmpty(roleIds), "Role ids can't be empty.");

		Account account = entityManager.get(Account.class, accountId);
		for (String item : roleIds) {
			account.addRole(entityManager.load(Role.class, Long.valueOf(item)));
		}
		entityManager.update(account);
	}

	@Override
	public void removeRoles(Long accountId, String... roleIds) {
		Assert.notNull(accountId, "Account id can't be empty.");
		Assert.isTrue(ArrayUtils.isNotEmpty(roleIds), "Role ids can't be empty.");

		Account account = entityManager.get(Account.class, accountId);
		for (String item : roleIds) {
			account.removeRole(entityManager.load(Role.class, Long.valueOf(item)));
		}
		entityManager.update(account);
	}

}
